﻿using NPOI.HSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDTO;
using WebIService;
using WebService;

namespace OAweb.Controllers
{
    public class HomeController : Controller
    {
        static List<JobDTO> last = new List<JobDTO>();
        ////ICO属性调用
        //第一种调用方式
        //public IJobService JobService { get; set; }
        //第二种调用方式
        private readonly IJobService JobService;
        public HomeController(IJobService jobService)
        {
            JobService = jobService;
        }
        public ActionResult Index()
        {
            var list = JobService.GetJobList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

          /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        public ActionResult Excel()
        {
            var lst = last;

            Dictionary<string, string> FiedNames = new Dictionary<string, string>();
            FiedNames.Add("名称", "Name");
            FiedNames.Add("代码", "Code");
            HSSFWorkbook wb = null;
            wb = Common.ExcleHelper.BuildSwitchData<JobDTO>("订单", lst, FiedNames);//调用通用方法
            string sExportFileName = "订单" + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";//设置下载文件的名称
            MemoryStream stream = new MemoryStream();
            wb.Write(stream);
            var buf = stream.ToArray();
            return File(buf, "pplication/vnd.ms-excel", sExportFileName);
        }

        public ActionResult Show()
        {
            return View();
        }
        /// <summary>
        /// 分页
        /// 模糊查询
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <param name="Name"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult ShowList(int limit, int offset, string Name, string sort, string Order, string Code)
        {
            int rowCount = 0;
            if (string.IsNullOrWhiteSpace(sort))
            {
                sort = "Id";
            }
            var list = JobService.GetPageJobList(limit, offset, Name, Code, sort, Order, ref rowCount).ToList();
            last = JobService.GetPageJobList(rowCount, 0, Name, Code, sort, Order, ref rowCount).ToList();
            return Json(new { total = rowCount, rows = list }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public ActionResult Add(string name, string code, string index)
        {
            if (index == "")
            {
                return Json(JobService.Add(name, code), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(JobService.up(name, code, index), JsonRequestBehavior.AllowGet);
            }

        }
        public ActionResult Error()
        {
            return View();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Del(string id)
        {
            var row = id.Split(',');
            int a = 0;
            foreach (var item in row)
            {
                if (JobService.Del(id))
                {
                    a += 1;
                }
            }
            if (a == row.Length)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }

    }
}